#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#include "nweb.h"
#include "nweb_log.h"
#include "worker.h"
#include "mime.h"
#include "authz.h"
#include "authn.h"
#include "http_request.h"
#include "http_response.h"
#include "http_protocol.h"

/**
 * this is a child web server process, so we can exit on errors
 * @param fd - file descriptor for the socket
 * @param hit - counter for browser hit
 */
void web(int fd, int hit)
{
    int res = 0;
	request_rec *r;
    r = read_request(fd, hit);
    
    register_authz(r);

    // if content need authentication
    if (run_auth_checker(r)) {
	register_authn_provider(r);
	res = run_check_user_id(r);
    }

    if (!res) { // if res is OK
	    analyze_request(r);
	    handle_resource(r);
    }
    
    // Clean up reequst_rec
    request_rec_destroy(r);
    
#ifdef LINUX
    sleep(1);	/* to allow socket to drain */
#endif
    exit(1);
}